package com.qsboss.demo.controller;

import com.qsboss.demo.domain.Result;
import com.qsboss.demo.pojo.TbRepayment;
import com.qsboss.demo.service.TbRepaymentService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

/**
 * @author 军哥
 * @version 1.0
 * @description: 还款记录控制层
 * @date 2023/10/25 10:32
 */

@Controller
@Slf4j
@RequestMapping(value = "/repay")
public class TbRepaymentController {

    @Autowired
    TbRepaymentService tbRepaymentService;

    /***
     * @description 提交还款
     * @return com.qsboss.demo.domain.Result
     * @author 军哥
     * @date 2023/10/25 15:53
     */
    @PostMapping(value = "/submitRepay")
    @ResponseBody
    public Result submitRepay(Integer billId) {
        return tbRepaymentService.submitRepay(billId);
    }

    @PostMapping(value = "/importPrompt")
    @ResponseBody
    public Result importPrompt(@RequestPart("file") MultipartFile file) {
        // 在控制台打印文件名
        String fileName = StringUtils.cleanPath(file.getOriginalFilename());
        System.out.println("上传的文件名: " + fileName);

        try {
            InputStream inputStream = file.getInputStream();
            HSSFWorkbook workbook = new HSSFWorkbook(inputStream);

            // 校验sheet
            HSSFSheet sheet = workbook.getSheet("还款记录表");
            if(sheet == null) {
                return Result.FAILED(500, "文件模板不正确");
            }

            // 校验表头

            // 读取数据
            // 获取到最大的行数
            int lastRowNum = sheet.getLastRowNum();
            for (int index = 1; index <= lastRowNum; index++) {
                HSSFRow row = sheet.getRow(index);
                // customerId
                HSSFCell cell0 = row.getCell(0);
                Integer customerId = (int)cell0.getNumericCellValue();
                // customerName
                HSSFCell cell1 = row.getCell(1);
                String customerName = cell1.getStringCellValue();
                // payAmount
                HSSFCell cell2 = row.getCell(2);
                double payAmount = cell2.getNumericCellValue();
                // billId
                HSSFCell cell3 = row.getCell(3);
                Integer billId = (int)cell3.getNumericCellValue();
                // optId
                HSSFCell cell4 = row.getCell(4);
                Integer optId = (int)cell4.getNumericCellValue();
                // optName
                HSSFCell cell5 = row.getCell(5);
                String optName = cell5.getStringCellValue();

                TbRepayment tbRepayment = new TbRepayment();
                tbRepayment.setCustomerId(customerId);
                tbRepayment.setCustomerName(customerName);
                tbRepayment.setPayAmount(payAmount);
                tbRepayment.setBillId(billId);
                tbRepayment.setOptId(optId);
                tbRepayment.setOptName(optName);
                tbRepayment.setPayAck(0);

                tbRepaymentService.addRepayment(tbRepayment);
            }

            return Result.SUCCESS();
        } catch (Exception e) {
            return Result.FAILED(500, "未知错误");
        }
    }

}
